package com.hy.controller;

import com.hy.bean.Manager;
import com.hy.bean.PageBean;
import com.hy.bean.RespBean;
import com.hy.dao.ManagerDao;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 *
 *
 * @Description:
 * @Author: zhangzhengwei2006
 * @Date: 2024-01-03 13:23
 *
 *
 */
public class AdminController {

    public String tologin(){
        return "forward:/WEB-INF/backPage/login.jsp";
    }
    public String toIndex(){
        return "forward:/WEB-INF/backPage/index.jsp";
    }

    public String toPersonage(){
        return "forward:/WEB-INF/backPage/subpage/personage.jsp";
    }

    public PageBean<Manager> login(String username, String password, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManagerDao managerDao = new ManagerDao();
        PageBean<Manager> managerPageBean = new PageBean<>();
        if (username.equals("") && password.equals("") || username == null && password == null){
            managerPageBean.setStatus(500);
            managerPageBean.setMsg("认真输入");
            return managerPageBean;
        }
        Manager manager = managerDao.selectByUsername(username);
        if (manager != null){
            if (manager.getSpassword().equals(password)){
                session.setAttribute("admin",manager.getSname());
                Cookie loginCookie = new Cookie("JSESSIONID", session.getId());
                loginCookie.setPath(request.getContextPath());
                loginCookie.setMaxAge(60*30); //30分钟，单位是秒
                response.addCookie(loginCookie);

                managerPageBean.setStatus(200);
                managerPageBean.setData(new Manager(manager.getSid(),manager.getSname(),null,manager.getSgender(),manager.getAvatar(),null,null,manager.getEnable(),null,null));
                // http://localhost:8080/demo042/back/admin?ac=toIndex
                return managerPageBean;
            }else {
                managerPageBean.setStatus(500);
                managerPageBean.setMsg("密码错误");
                return managerPageBean;
            }
        }else {
            managerPageBean.setStatus(500);
            managerPageBean.setMsg("用户名或密码错误");
            // http://localhost:8080/demo042/back/admin?ac=toIndex
            return managerPageBean;
        }
    }

    // 退出登录
    public boolean logout(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
        session.removeAttribute("admin");// 清除session
        if (session.getAttribute("admin") == null) {
            Cookie loginCookie = new Cookie("JSESSIONID", null);
            loginCookie.setMaxAge(0);
            response.addCookie(loginCookie);
            return true;
        }
        return false;
    }

    // 查询个人信息
    public PageBean<Manager> getAdmin(Integer id) throws Exception {
        ManagerDao managerDao = new ManagerDao();
        PageBean<Manager> managerPageBean = new PageBean<>();
        Manager manager = managerDao.selectAdmin(id);
        manager.setTelephone(manager.getTelephone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));// 手机号脱敏
        managerPageBean.setCode(200);
        managerPageBean.setData(new Manager(manager.getSid(), manager.getSname(), null, manager.getSgender(), manager.getAvatar(), manager.getMailbox(), manager.getTelephone(), manager.getEnable(), manager.getSentrydate(), null));
        return managerPageBean;
    }

    // 修改密码时用
    public RespBean getPassword(Integer id) throws Exception {
        ManagerDao managerDao = new ManagerDao();
        return RespBean.success(null, managerDao.selectAdminPassword(id));
    }

    // 修改密码
    public RespBean updatePassword(Integer id,String password) throws Exception {
        ManagerDao managerDao = new ManagerDao();
        return managerDao.updatePassword(id,password);
    }

    // 修改用户
    public RespBean updateAdmin(Integer id, String sname, String sgender, String telephone, String mailbox) throws Exception {
        Manager manager = new Manager(id, sname, null, sgender, null, mailbox, telephone, "0", null, null);
        if (sgender.equals("男")){
            manager.setSgender("M");
        } else if (sgender.equals("女")) {
            manager.setSgender("F");
        }
        ManagerDao managerDao = new ManagerDao();
        return managerDao.updateManager(manager);
    }

}
